package com.solution.admin.customer.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.solution.admin.customer.entity.DeliveryModeEntity;
import com.solution.admin.customer.service.DeliveryModeService;
import com.solution.common.controller.BaseController;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 单位配件方式web控制层
 * @author solin
 * @date 2017-10-29 21:56:05
 */
@RequestMapping("/deliveryMode")
@Controller
public class DeliveryModeController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(DeliveryModeController.class);

	@Autowired
	private DeliveryModeService deliveryModeService;
	
	/**
	 * 跳转单位配件方式查询页面
	 * @param DeliveryMode 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author solin
 	 * @date 2017-10-29 21:56:05
	 */
	@RequestMapping("/toListDeliveryModePage")
	public String toListDeliveryModePage(DeliveryModeEntity deliveryMode, HttpServletRequest request) {
		return "customer/deliveryMode_list";
	}

	/**
	 * 分页查询单位配件方式列表
	 * @param deliveryMode 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author solin
 	 * @date 2017-10-29 21:56:05
	 */
	@RequestMapping("/listDeliveryModePage")
	@ResponseBody
	public JSONGrid listDeliveryModePage(DeliveryModeEntity deliveryMode, HttpServletRequest request){
		int pageNum=1;
		int numPerPage=100;
		if (StringUtil.isNotEmpty(request.getParameter("page"))) {
			pageNum=Integer.parseInt(request.getParameter("page").toString());
		}
		if (StringUtil.isNotEmpty(request.getParameter("rows"))) {
			numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		}
		
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("companyId", request.getParameter("companyId"));
		try {
			PageBean pageBean = deliveryModeService.listDeliveryModePage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询单位配件方式失败", logger, e);
		}
		return new JSONGrid();
	}
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author solin
 	 * @date 2017-10-29 21:56:05
	 */
	@RequestMapping("/toAddDeliveryMode")
	public String toAddDeliveryMode(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "customer/deliveryMode_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author solin
 	 * @date 2017-10-29 21:56:05
	 */
	@RequestMapping("/toUpdateDeliveryMode")
	public String toUpdateDeliveryMode(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			DeliveryModeEntity deliveryMode = deliveryModeService.getDeliveryModeById(id);
			request.setAttribute("deliveryMode", deliveryMode);
		} catch (Exception e) {
			handleException("跳转编辑单位配件方式页面错误", logger, e);
		}
		return "customer/deliveryMode_form";
	}

	/**
	 * 新增或保存单位配件方式
	 * @param request 请求对象
	 * @param deliveryModeEntity 单位配件方式实体
	 * @return
	 * @author solin
 	 * @date 2017-10-29 21:56:05
	 */
	@RequestMapping("/saveOrUpdateDeliveryMode")
	@ResponseBody
	public Map<String, Object> saveDeliveryMode(HttpServletRequest request, DeliveryModeEntity deliveryModeEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			String id = request.getParameter("id");
			if(StringUtil.isNotEmpty(id)){
				result = deliveryModeService.updateDeliveryMode(deliveryModeEntity);
			}else{
				String strJson = request.getParameter("data");
				List<DeliveryModeEntity> modeList = JSONArray.parseArray(strJson, DeliveryModeEntity.class);
				for (int i = 0; i < modeList.size(); i++) {
					modeList.get(i).setCreater(this.getUserCode());
				}
				result = (int)deliveryModeService.saveDeliveryMode(modeList);
			}
			resultMap.put("code", result);
		} catch (Exception e) {
			handleException("添加单位配件方式失败", logger, e);
		}
		return resultMap;
	}
	
}
